package com.jkkc.lib.base.core;

import static com.jkkc.lib.base.util.logcat.LogcatObserverKt.LOGCAT_TYPE_ALL;
import static com.jkkc.lib.base.util.logcat.LogcatObserverKt.LOGCAT_TYPE_SINGLE;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;

import com.alibaba.android.arouter.launcher.ARouter;
import com.jkkc.lib.base.BuildConfig;
import com.jkkc.lib.base.constant.Constant;
import com.jkkc.lib.base.util.AppManager;
import com.jkkc.lib.base.util.GlideUtils;
import com.jkkc.lib.base.util.Utils;
import com.jkkc.lib.base.util.WriterLocalUtils;
import com.jkkc.lib.base.util.logcat.LogcatObserver;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by goldze on 2017/6/15.
 */

public class BaseApplication extends Application {

    /**
     * 需要收集日志的Activity
     */
    public static List<String> recorderLogActivity = new ArrayList<>();

    private static Application sInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        setApplication(this);
        if (BuildConfig.DEBUG) {
            ARouter.openLog();
            ARouter.openDebug();
        }
        ARouter.init(sInstance);
        GlideUtils.glideRegistry();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        ARouter.getInstance().destroy();
    }

    /**
     * 当主工程没有继承BaseApplication时，可以使用setApplication方法初始化BaseApplication
     */
    public static synchronized void setApplication(@NonNull Application application) {
        sInstance = application;
        //初始化工具类
        Utils.init(application);
        //注册监听每个activity的生命周期,便于堆栈式管理
        application.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                AppManager.addActivity(activity);
                recordLogcat(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {
            }

            @Override
            public void onActivityResumed(Activity activity) {
            }

            @Override
            public void onActivityPaused(Activity activity) {
            }

            @Override
            public void onActivityStopped(Activity activity) {
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                AppManager.removeActivity(activity);
                WriterLocalUtils.writeOperation("离开" + activity.getLocalClassName() + "页面");
            }
        });
    }

    /**
     * 记录Logcat日志
     */
    private static void recordLogcat(Activity activity) {
        String name = activity.getClass().getSimpleName();
        WriterLocalUtils.writeOperation("进入" + name + "页面");
        if (activity instanceof AppCompatActivity) {
            Lifecycle lifecycle = ((AppCompatActivity) activity).getLifecycle();
            if (!recorderLogActivity.isEmpty() && name.contains(recorderLogActivity.get(0))) {
                String filePath = Constant.getLOGCAT_LOG_APP() + "中长跑.log";
                lifecycle.addObserver(LogcatObserver.getLogcatUtils(LOGCAT_TYPE_ALL, filePath));
                WriterLocalUtils.writeOperation("全局日志开始记录");
            } else if (recorderLogActivity.contains(name)) {
                String filePath = Constant.getLOGCAT_LOG_PROJECT() + name + ".log";
                lifecycle.addObserver(LogcatObserver.getLogcatUtils(LOGCAT_TYPE_SINGLE, filePath));
                WriterLocalUtils.writeOperation(name + "，日志开始记录");
            }
        }
    }

    /**
     * 获得当前app运行的Application
     */
    public static Application getInstance() {
        if (sInstance == null) {
            throw new NullPointerException("please inherit BaseApplication or call setApplication.");
        }
        return sInstance;
    }
}
